﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Budget.SpendingWCFProxy;
using Budget.HelperClasses;
using Newtonsoft.Json;

namespace Budget.Data
{
	public partial class Spendings : System.Web.UI.Page
	{
		protected void Page_Load( object sender, EventArgs e )
		{
			string action = Request.Params["xaction"];
			if ( !string.IsNullOrEmpty( action ) )
			{
				switch ( action )
				{
					case "create":
						CreateSpending();
						break;
					case "update":
						//UpdateCategory();
						break;
					case "getmonthsummary":
						GetMonthSummary();
						break;
					case "listspendingsbymonthcategoryid":
						ListSpendingsByMonthCategoryID();
						break;
					default:
						ListSpendings();
						break;
				}
			}
		}

		private void GetMonthSummary()
		{
			JsonResponse<MonthSummary> response = new JsonResponse<MonthSummary>();

			try
			{
				DateTime analysisDate = Convert.ToDateTime( Request.Params["AnalysisDate"] );

				WCFHelper.Using<SpendingServiceClient>( svc => response.Data = svc.GetMonthCategorySummary( analysisDate ) );

				response.Count = response.Data.Count();
				response.Success = true;
				response.Message = "Successfully created Month Category Summary objects";
			}
			catch ( Exception ex )
			{
				response.Success = false;
				response.Message = "An error occurred while creating Month Category Summary objects.";
			}

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );
		}

		private void CreateSpending()
		{
			Spending newSpending = JsonConvert.DeserializeObject<Spending>( Request.Form["data"] );
			newSpending.Timestamp = DateTime.Now;

			JsonResponse<Spending> response = new JsonResponse<Spending>();

			try
			{
				WCFHelper.Using<SpendingServiceClient>( svc => svc.Save( newSpending ) );

				response.Success = true;
				response.Message = "Successfully created Mth object.";
			}
			catch ( Exception ex )
			{
				response.Success = false;
				response.Message = "An error occurred while creating Mth object.";
			}

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );
		}

		private void ListSpendings()
		{
			JsonResponse<Spending> response = new JsonResponse<Spending>();

			WCFHelper.Using<SpendingServiceClient>( svc => response.Data = svc.GetSpendingsByMonthAndYear( DateTime.Now.Month, DateTime.Now.Year ) );

			response.Count = response.Data.Count();
			response.Success = true;
			response.Message = "Successfully ";

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );
		}

		private void ListSpendingsByMonthCategoryID()
		{
			JsonResponse<Spending> response = new JsonResponse<Spending>();

			try
			{
				int mthCategoryId = Convert.ToInt32( Request.Params["mthcategoryid"] );

				WCFHelper.Using<SpendingServiceClient>( svc => response.Data = svc.GetSpendingsByMthCategoryId( mthCategoryId ) );

				response.Count = response.Data.Count();
				response.Success = true;
				response.Message = "Successfully created Month Category Summary objects";
			}
			catch ( Exception ex )
			{
				response.Success = false;
				response.Message = "An error occurred while creating Month Category Summary objects.";
			}

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );

		}
	}
}
